#!/bin/bash

# AI测试助手 2.0 快速启动脚本

echo "🚀 AI测试助手 2.0 启动脚本"
echo "============================"

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ Docker未安装，请先安装Docker"
    exit 1
fi

# 检查Docker Compose是否安装
if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose未安装，请先安装Docker Compose"
    exit 1
fi

# 创建必要的目录
echo "📁 创建必要的目录..."
mkdir -p uploads logs sql nginx/conf.d ssl

# 创建环境变量文件
if [ ! -f backend/.env ]; then
    echo "⚙️  创建后端环境变量文件..."
    cat > backend/.env << EOF
# 数据库配置
DATABASE_URL=mysql+pymysql://app_user:app_password@mysql:3306/ai_testing_assistant

# 安全配置
SECRET_KEY=your-secret-key-change-in-production-$(date +%s)
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=43200

# 应用配置
DEBUG=True
HOST=0.0.0.0
PORT=8000

# CORS配置
ALLOWED_HOSTS=["http://localhost:3000", "http://127.0.0.1:3000"]

# 文件上传配置
UPLOAD_DIR=uploads
MAX_FILE_SIZE=52428800

# AI配置 (请填入您的API密钥)
OPENAI_API_KEY=
OPENAI_BASE_URL=https://api.openai.com/v1
AI_MODEL=gpt-3.5-turbo

# 日志配置
LOG_LEVEL=INFO
LOG_FILE=logs/app.log
EOF
fi

if [ ! -f frontend/.env ]; then
    echo "⚙️  创建前端环境变量文件..."
    cat > frontend/.env << EOF
# API地址
VITE_API_BASE_URL=http://localhost:8000

# 应用配置
VITE_APP_TITLE=AI测试助手
EOF
fi

# 创建数据库初始化脚本
if [ ! -f sql/init.sql ]; then
    echo "🗄️  创建数据库初始化脚本..."
    cat > sql/init.sql << EOF
-- 创建数据库
CREATE DATABASE IF NOT EXISTS ai_testing_assistant CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 使用数据库
USE ai_testing_assistant;

-- 创建应用用户
CREATE USER IF NOT EXISTS 'app_user'@'%' IDENTIFIED BY 'app_password';
GRANT ALL PRIVILEGES ON ai_testing_assistant.* TO 'app_user'@'%';
FLUSH PRIVILEGES;
EOF
fi

# 创建Nginx配置
if [ ! -f nginx/conf.d/default.conf ]; then
    echo "🌐 创建Nginx配置..."
    mkdir -p nginx/conf.d
    cat > nginx/conf.d/default.conf << EOF
upstream frontend {
    server frontend:3000;
}

upstream backend {
    server backend:8000;
}

server {
    listen 80;
    server_name localhost;

    # 前端代理
    location / {
        proxy_pass http://frontend;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }

    # 后端API代理
    location /api {
        proxy_pass http://backend;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }

    # 后端文档代理
    location /docs {
        proxy_pass http://backend;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }

    location /redoc {
        proxy_pass http://backend;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }
}
EOF
fi

# 创建后端Dockerfile
if [ ! -f backend/Dockerfile ]; then
    echo "🐳 创建后端Dockerfile..."
    cat > backend/Dockerfile << EOF
FROM python:3.9-slim

WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y \\
    gcc \\
    default-libmysqlclient-dev \\
    pkg-config \\
    && rm -rf /var/lib/apt/lists/*

# 复制依赖文件
COPY backend_requirements.txt requirements.txt

# 安装Python依赖
RUN pip install --no-cache-dir -r requirements.txt

# 复制应用代码
COPY . .

# 暴露端口
EXPOSE 8000

# 启动命令
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
EOF
fi

# 创建前端Dockerfile
if [ ! -f frontend/Dockerfile ]; then
    echo "🐳 创建前端Dockerfile..."
    cat > frontend/Dockerfile << EOF
FROM node:18-alpine

WORKDIR /app

# 复制依赖文件
COPY package*.json ./

# 安装依赖
RUN npm ci

# 复制应用代码
COPY . .

# 暴露端口
EXPOSE 3000

# 启动命令
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]
EOF
fi

echo "🔧 配置完成！"
echo ""
echo "请按照以下步骤继续："
echo "1. 如需使用AI功能，请在 backend/.env 文件中配置 OPENAI_API_KEY"
echo "2. 运行以下命令启动服务："
echo "   docker-compose up -d"
echo ""
echo "3. 访问以下地址："
echo "   - 前端应用: http://localhost:3000"
echo "   - 后端API: http://localhost:8000"
echo "   - API文档: http://localhost:8000/docs"
echo ""
echo "4. 默认管理员账号将在首次启动时创建："
echo "   用户名: admin"
echo "   密码: admin123"
echo ""
echo "🎉 准备就绪！执行 'docker-compose up -d' 开始使用吧！"
EOF

chmod +x start.sh 